package com.gzdh.dishrecognition.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.entity.BmcmEntity;
import com.gzdh.dishrecognition.model.WeekMenuBakModel;

import java.util.ArrayList;
import java.util.List;

public class LeftBmzlAdapter extends RecyclerView.Adapter<LeftBmzlAdapter.ViewHolder> {

    private Context mcontext;

    private List<WeekMenuBakModel> list;

    private int select = -1;

    private UserOnClick userOnClick;

    public LeftBmzlAdapter(Context context) {
        mcontext = context;
        this.list = new ArrayList<>();
    }

    public void setUserOnClick(UserOnClick userOnClick) {
        this.userOnClick = userOnClick;
    }

    public void setList(List<WeekMenuBakModel> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_string_gridview_item1, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        holder.nameTv.setText(list.get(position).getName().trim());

        if (position == select) {
            holder.mainLinear.setBackgroundColor(Color.argb(150, 80, 80, 80));
        } else {
            holder.mainLinear.setBackgroundColor(Color.argb(0, 255, 255, 255));
        }

        inSelect(holder, position);

    }

    private void inSelect(ViewHolder holder, int position) {
        holder.mainLinear.setOnClickListener(view -> {
            select = position;
            notifyDataSetChanged();
            if (userOnClick != null) {
                userOnClick.userOnClick(list.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView nameTv;
        LinearLayout mainLinear;


        ViewHolder(View item) {
            super(item);
            nameTv = item.findViewById(R.id.nameTv);
            mainLinear = item.findViewById(R.id.mainLinear);

        }

    }

    public interface UserOnClick {
        void userOnClick(WeekMenuBakModel weekMenuBakModel);
    }

}
